﻿using Protocol.Code;
using Protocol.Dto;
using Script.Framework;
using Script.UI.Msg;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI._0.Login
{
    public class RegisterPanel : UIBase
    {
        private void Awake()
        {
            //Debug.Log("我他妈运行了");
            Bind(UIEvent.REGISTER_REALIST_PANEL_ACTIVE);
        }

        // Start is called before the first frame update
        void Start()
        {
            // 获取对象
            btnRegister = transform.Find("Btn_Register").GetComponent<Button>();
            btnClose = transform.Find("Btn_Close").GetComponent<Button>();
            inputAccount = transform.Find("Account").GetComponent<InputField>();
            inputPassword = transform.Find("Password").GetComponent<InputField>();
            inputRepeat = transform.Find("Repeat").GetComponent<InputField>();

            // 注册事件
            btnClose.onClick.AddListener(closeClick);
            btnRegister.onClick.AddListener(registerClick);

            // 创建对象
            promptMsg = new PromptMsg();
            socketMsg = new SocketMsg();

            SetPanelActive(false);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.REGISTER_REALIST_PANEL_ACTIVE:
                    SetPanelActive((bool) message);
                    break;
            }
        }

        private Button btnRegister;
        private Button btnClose;
        private InputField inputAccount;
        private InputField inputPassword;
        private InputField inputRepeat;

        private PromptMsg promptMsg;
        private SocketMsg socketMsg;

        public override void OnDestroy()
        {
            base.OnDestroy();

            btnClose.onClick.RemoveListener(registerClick);
            btnClose.onClick.RemoveListener(closeClick);
        }

        private void registerClick()
        {
            if (string.IsNullOrEmpty(inputAccount.text))
            {
                promptMsg.Change("注册账号不能为空", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);

                return;
            }

            if (string.IsNullOrEmpty(inputPassword.text) ||
                inputPassword.text.Length < 4 ||
                inputPassword.text.Length > 16)
            {
                promptMsg.Change("注册账号不能为空", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                return;
            }

            if (string.IsNullOrEmpty(inputRepeat.text) ||
                inputPassword.text != inputRepeat.text)
            {
                promptMsg.Change("两次输入密码不一致", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                return;
            }

            AccountDto dto = new AccountDto(inputAccount.text, inputPassword.text);

            socketMsg.Change(OpCode.ACCOUNT, AccountCode.REGIST_CREQ, dto);

            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void closeClick()
        {
            SetPanelActive(false);
        }
    }
}